﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DomainModel;
using Utils;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;

namespace WebUI.Models
{
    public class IndShotViewModel: BaseViewModel
    {
        public IndShotViewModel(Shot shot)
            : base(new { Shot = shot})
        { }

        public Shot Shot { get; set; }
        public Shot PrevShot { get; set; }
        public Shot NextShot { get; set; }

        public string AvatarUrl { get; set; }
        public string AvatarUrl32 { get; set; }
        public Login Login { get; set; }
        public Ind Ind { get; set; }

        public string ShotUrl { get; set; }
        public List<STag> ShotTagList { get; set; }
        

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            Shot = dynParams.Shot;

            IndLogin indLogin = DataUtils.GetIndLoginByIndID(Shot.IndID);
            Login = indLogin.Login;
            Ind = indLogin.Ind;

            ShotUrl = ImageUtils.GetShotRelativeUrl(Ind.IndID, Shot.ShotID, ShotSizeEnum.Shot_900x600, Shot.FileExtension);
            AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.IndAvatar, Ind.IndID, AvatarSizeEnum.Avatar_48x48, Ind.AvatarFileExtension);
            AvatarUrl32 = ImageUtils.GetAvatarRelativeUrl(AvatarType.IndAvatar, Ind.IndID, AvatarSizeEnum.Avatar_32x32, Ind.AvatarFileExtension);
            ShotTagList = SqlUtilities.Query<STag>(ConstUtils.SP_GetShotTagListByID, new { IndID = Ind.IndID, ShotID = Shot.ShotID }).ToList();
            //build prevShot, and nextshot
            var shotNeighborList = DataUtils.GetList<Shot>(ConstUtils.SP_GetShotNeighbor, new { Ind.IndID, Shot.ShotID });
            if (shotNeighborList.Count == 3)
            {
                PrevShot = shotNeighborList[0];
                NextShot = shotNeighborList[2];
            }
            else if (shotNeighborList.Count == 2)
            {
                if (shotNeighborList[0].ShotID == Shot.ShotID)
                {
                    NextShot = shotNeighborList[1];
                }
                else
                {
                    PrevShot = shotNeighborList[0];
                }
            }
            if (PrevShot != null)
            {
                PrevShot["ShotUrl"] = ImageUtils.GetShotRelativeUrl(Ind.IndID, PrevShot.ShotID, ShotSizeEnum.Shot_90x60, PrevShot.FileExtension);
            }
            if (NextShot != null)
            {
                NextShot["ShotUrl"] = ImageUtils.GetShotRelativeUrl(Ind.IndID, NextShot.ShotID, ShotSizeEnum.Shot_90x60, NextShot.FileExtension);
            }
            //end here

        }

        
    }
}